<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import router from '@/router/index'
import { employeeLogin, employeeRegister } from '@/api/employee';
import {useEmployeeStore} from '@/stores/modules/employee'
import { ElMessage } from 'element-plus';
const employeeStore=useEmployeeStore()
const isRegister = ref(true)
const formModel=ref({
    username:"",
    password:"",
    repassword:""
})
const form=ref()
//整个表单的校验规则
const rules = {
  //required：非空校验
  //min,max：长度校验
  //正则校验
  //自定义校验
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    },
  ],
  repassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        //判断value和当前form收集的password是否一致
        if (value !== formModel.value.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      }
    }
  ]
}

const login=async()=>{
    //表单预校验
    await form.value.validate()
    //登录功能
    console.log(formModel.value);
    const res= await employeeLogin(formModel.value);
    //程序执行到这里说明登录成功
    employeeStore.setToken(res.data.data.token)
    ElMessage.success("登录成功")
    router.push('/')
}

const register=async()=>{
    //表单预校验
    await form.value.validate()
    console.log(formModel.value);
    //注册功能
    const res=await employeeRegister(formModel.value);
    isRegister.value=false
    ElMessage.success("注册成功")
    router.push('/login')
}
</script>

<template>
    <el-row class="login-page">
        <el-col :span="12" class="bg"></el-col>
        <el-col :span="6" :offset="3" class="form">
            <el-card class="card">
                <el-form :model="formModel" :rules="rules" ref="form" size="large" autocomplete="off" v-if="isRegister">
                    <el-form-item>
                        <h1 class="word">注册</h1>
                    </el-form-item>
                    <el-form-item prop="username">
                        <el-input v-model="formModel.username" :prefix-icon="User" placeholder="请输入用户名"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="formModel.password" :prefix-icon="Lock" type="password" placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item prop="repassword">
                        <el-input v-model="formModel.repassword" :prefix-icon="Lock" type="password" placeholder="请输入再次密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="register" class="button" type="primary" auto-insert-space>
                            注册
                        </el-button>
                    </el-form-item>
                    <el-form-item class="flex">
                        <el-link type="info" :underline="false" @click="isRegister = false">
                            ← 返回
                        </el-link>
                    </el-form-item>
                </el-form>
                <el-form :model="formModel" :rules="rules" ref="form" size="large" autocomplete="off" v-else>
                    <el-form-item>
                        <h1 class="word">登录</h1>
                    </el-form-item>
                    <el-form-item prop="username">
                        <el-input v-model="formModel.username" :prefix-icon="User" placeholder="请输入用户名"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input v-model="formModel.password" name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item class="flex">
                        <div class="flex">
                            <el-checkbox>记住我</el-checkbox>
                            <el-link type="primary" :underline="false">忘记密码？</el-link>
                        </div>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="login" class="button" type="primary" auto-insert-space>登录</el-button>
                    </el-form-item>
                    <el-form-item class="flex">
                        <el-link type="info" :underline="false" @click="isRegister = true">
                            注册 →
                        </el-link>
                    </el-form-item>
                </el-form>
            </el-card>
        </el-col>
    </el-row>
</template>

<style lang="scss" scoped>
.login-page {
    height: 100vh;
    background-color: rgba(255, 255, 255, 1);
    .word {
        font-size: 30px;
        /*设置文字大小*/
        color: #3366FF;
        /*设置文字颜色*/
        text-shadow: 0 8px 10px #6699FF;
        /*设置文字阴影*/
        font-weight: bolder;
        /*设置文字宽度*/
        text-align: center;
        /*设置文字居中*/
    }

    .bg {
        // 使用一个容器来包含图片和渐变层
        position: relative;
        width: 100%; // 确保容器占据整个宽度
        height: 100%; // 确保容器占据整个高度
        // 使用背景图片，并添加一个右侧的白色渐变层
        background:
            linear-gradient(to left, rgba(255, 255, 255, 1) 0.1%, rgba(255, 255, 255, 0) 100%),
            url('@/assets/login2.png') no-repeat 60% center / 820px auto,
            url('@/assets/login_law.jpeg') no-repeat center / cover;
        // 将图片定位到容器的中心，并确保它不会超出容器的边界
        background-position: center center;
        // 确保背景图片不重复，并且覆盖整个容器（尽管由于渐变层的存在，它实际上不会完全覆盖）
        background-size: cover;
        // 保留右侧的边框半径，但这里可能需要根据实际布局进行调整
        border-radius: 0 20px 20px 0;
        // 可选：添加一个内边距，以确保内容不会紧贴图片边缘
        padding-right: 20px; // 根据需要调整这个值

        // 隐藏溢出的内容，以防图片或渐变层超出容器
        overflow: hidden;
    }

    .form {
        display: flex;
        flex-direction: column;
        justify-content: center;
        user-select: none;

        .title {
            margin: 0 auto;
        }

        .button {
            width: 100%;
        }

        .flex {
            width: 100%;
            display: flex;
            justify-content: space-between;
        }

    }
    .card{
        background: url('@/assets/whiteBack.jpg');
    }
}
</style>